import {
  plantByName,
  plantById,
  plantByGenusId,
  plantByMonth,
  getPlantByGenusName,
  plantByBreedId,
  plantByFamily
} from "@/api/plant";

const getDefaultState = () => {
  return {};
};

const state = getDefaultState();

const mutations = {};

const actions = {
  //按属目查询
  async plantByGenusId({ state }, genusId) {
    return new Promise((resolve, reject) => {
      plantByGenusId(genusId).then(response => {
        if (response.success) {
          resolve(response.data);
        } else {
          reject(response);
        }
      });
    });
  },
  //根据名称
  async plantByName({ state }, name) {
    return new Promise((resolve, reject) => {
      plantByName(name).then(response => {
        if (response.success) {
          resolve(response.data);
        } else {
          reject(response);
        }
      });
    });
  },
  //根据月份
  async plantByMonth({ state }, month) {
    return new Promise((resolve, reject) => {
      plantByMonth(month).then(response => {
        if (response.success) {
          resolve(response.data);
        } else {
          reject(response);
        }
      });
    });
  },
  //根据属性名
  async getPlantByGenusName({ state }, name) {
    return new Promise((resolve, reject) => {
      getPlantByGenusName(name).then(response => {
        if (response.success) {
          resolve(response.data);
        } else {
          reject(response);
        }
      });
    });
  },
  //根据id查询种植信息
  async plantByBreedId({ state }, plantId) {
    return new Promise((resolve, reject) => {
      plantByBreedId(plantId).then(response => {
        if (response.success) {
          resolve(response.data);
        } else {
          reject(response);
        }
      });
    });
  },
  //查看所有科
  async plantById({ state }, id) {
    return new Promise((resolve, reject) => {
      plantById(id).then(response => {
        if (response.success) {
          resolve(response.data);
        } else {
          reject(response);
        }
      });
    });
  },
  //根据科目查询
  async plantByFamily({ state }, name) {
    return new Promise((resolve, reject) => {
      plantByFamily(name).then(response => {
        if (response.success) {
          resolve(response.data);
        } else {
          reject(response);
        }
      });
    });
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  actions
};
